// pages/index/swiper/swiper.js
import { list } from '../../../../api/ad'
const util = require('../../../../utils/util')
let app = getApp();
Component({



  // 以下是旧式的定义方式，可以保持对 <2.2.3 版本基础库的兼容
  attached: function () {

    // 在组件实例进入页面节点树时执行

  },
  detached: function () {
    // 在组件实例被从页面节点树移除时执行
  },
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },
  lifetimes: {
    attached() {
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.setData({
        tagIds: this.data.tagIds
      })

      this.loadAd()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tagIds: [],
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    photoList: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 500,
    previousMargin: 0,
    color: [],
    c: '',
    nextMargin: 0,
    pageNum: 0,
    pageSize: 10,

  },

  /**
   * 组件的方法列表
   */
  created() {

  },
  methods: {
    test() {
      util.token(app.globalData.token)
    },
    toLink(e) {
      if (e.currentTarget.dataset.type.hasOwnProperty('typeId') && !util.isNull(e.currentTarget.dataset.type.typeId)) {
        if (e.currentTarget.dataset.type.type === 0) {
          wx.navigateTo({
            url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.type.typeId
          })
        } else if (e.currentTarget.dataset.type.type === 9) {
          wx.navigateTo({
            url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.type.typeId
          })
        } else if (e.currentTarget.dataset.type.type === 2) {
          wx.navigateTo({
            url: '/pages/member/detail/detail?id=' + e.currentTarget.dataset.type.typeId,
          })
        } else if (e.currentTarget.dataset.type.type === 1) {
          wx.navigateTo({
            url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.type.typeId
          })
        } else if (e.currentTarget.dataset.type.type === 12) {
          console.log(e.currentTarget.dataset.type)
          const { typeName, url } = e.currentTarget.dataset.type
          if (typeName == "fill://" || url == "fill://") {
            wx.navigateTo({
              url: '/pages/wallet/jumpPage/saveCard/recharge',
            })
          }
          /** 
           * 打开另一个小程序
           * appId，path
           **/
          if (typeName.indexOf("mp://") == 0 || url.indexOf("mp://") == 0) {
            if ((typeName.indexOf("appId") > 0 || url.indexOf("appId") > 0) && (typeName.indexOf("path") > 0 || url.indexOf("path") > 0)) {
              let appId_index = typeName.indexOf('appId=',)
              let path_index = typeName.indexOf('&path=',)
              let appId = typeName.substring(appId_index + 6, path_index)
              let path = typeName.substring(path_index + 6)
              // console.log(appId, path, typeName.length)
              wx.navigateToMiniProgram({
                appId: appId,
                path: path,
                success: res => {
                  console.log(res)
                },
                fail: err => {
                  console.log(err)
                },
              })
            }
          }
        }
      } else if (e.currentTarget.dataset.type.type === 12) {
        const { typeName, path } = e.currentTarget.dataset.type
        if (typeName == "fill://" || path == "fill://") {
          wx.navigateTo({
            url: '/pages/wallet/jumpPage/saveCard/recharge',
          })
        }
        /** 
         * 打开另一个小程序
         * appId，path
         **/
        if (typeName.indexOf("mp://") == 0 || path.indexOf("mp://") == 0) {
          if ((typeName.indexOf("appId") > 0 || path.indexOf("appId") > 0) && (typeName.indexOf("path") > 0 || path.indexOf("path") > 0)) {
            let appId_index = typeName.indexOf('appId=',)
            let path_index = typeName.indexOf('&path=',)
            let appId = typeName.substring(appId_index + 6, path_index)
            let path = typeName.substring(path_index + 6)
            wx.navigateToMiniProgram({
              appId: appId,
              path: path,
              success: res => {
                console.log(res)
              },
              fail: err => {
                console.log(err)
              },
            })
          }
        }
      }
    },
    searchClick() {
      wx.navigateTo({
        url: '/pages/goods/search/index'
      })
    },
    changeBackground(e) {
      this.setData({
        c: this.data.color[e.detail.current]
      })
    },
    detailsClick() {
      wx.navigateTo({
        url: '/pages/goods/product/details',
      })
    },

    loadAd() {
      var _this = this
      list({
        mchId: app.globalData.mchId,
        tagIds: this.data.tagIds
      }).then(res => {
        res.data.data.forEach(element => {
          element.backgroundColor = "linear-gradient(180deg," + element.backgroundColor + ", rgba(248, 248, 248, 1))"
          _this.data.color.push(element.backgroundColor)
          element.thumbnail = util.thumbnail(element.thumbnail, 710, 355)
          this.data.c = this.data.color[0]
        });
        _this.data.photoList = res.data.data
        this.setData({
          color: this.data.color,
          photoList: _this.data.photoList,
          c: this.data.c,
          statusBarHeight: app.globalData.statusBarHeight,
        });
      });
    }
  }
})
